package com.me.libgdx_test;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class ActivitySelection implements Screen {
	
	ActivityMain game;
	Stage stage;
	SpriteBatch batch;
	ClassSkinReference skin;
	ClassLogDisplay logger;
	float width, height;
	ClassPreviewActor preview_window;
	ClassReferenceDrawable references;
	final ClassSelectionDirector director;
	//TextField textfield
	
	public ActivitySelection(final ActivityMain game){
		this.game = game;
		logger = new ClassLogDisplay("ActivityIndex");
		logger.print(String.valueOf(Gdx.graphics.getWidth()) + "x" + String.valueOf(Gdx.graphics.getHeight()));
		width = Gdx.graphics.getWidth();
		height = Gdx.graphics.getHeight();
		
		batch = new SpriteBatch();
		stage = new Stage();
		skin = game.skin;
		
		final Label title_label = new Label("Choose Class", skin.get_skin());
		title_label.setPosition(3*width/20, 5*height/6);
		title_label.setSize(width/10, height/10);
		title_label.setFontScale(10);

		Label detail_field = new Label("", skin.get_skin());
		detail_field.setPosition(27*width/40, 28*height/100);
		detail_field.setSize(4*width/20,3*height/6);
		detail_field.setFontScale(1.5f);
		detail_field.setWrap(true);
		
		director = new ClassSelectionDirector(title_label,detail_field,false,0);
		
		init_things();
		
		Gdx.input.setInputProcessor(stage);
	}

	public void init_things(){
		init_classes();
		init_actors();
	}
	
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
		stage.draw();
		Table.drawDebug(stage);
		
	}

	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, false);
		
	}

	@Override
	public void show() {
		director.reset_index();
		Gdx.input.setInputProcessor(stage);
		init_actors();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		stage.dispose();
		//skin.get_skin().dispose();
	}
	
	public void init_classes(){
		ClassTexturePackerLoader sprite_textures = game.sprite_textures;
		references = new ClassReferenceDrawable(0,0,0,game.unlockable);
		ClassGalleryActor classes = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(4*height/6)),
				new Point((int)width/20,(int)(13*height/20)),
				8,3);
		classes.set_references(references.classObjects);
		ClassGalleryActor races = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(3*height/6)),
				new Point((int)width/20,(int)(12*height/20)),
				5,5);
		races.set_references(references.raceObjects);
		ClassGalleryActor levels = new ClassGalleryActor(sprite_textures,
				new Point((int)width/3,(int)(3*height/6)),
				new Point((int)width/20,(int)(12*height/20)),
				5,5);
		levels.set_references(references.levels_w_description());
		director.set_actors(races,levels,classes,game.unlockable.get_unlockable_prototypes().get_factory());
	}
	
	public void init_actors(){
		//ArrayList<Sprite> sprites = new ArrayList<Sprite>();
		//sprites.add(sprite_textures.find_by_name("class"));

		stage.clear();
		
		stage.addActor(director.get_title());
		stage.addActor(director.get_description());
		
		final TextButton Start_Quest = new TextButton("", skin.get_button_skin());
		//LEFT = 0, RIGHT = 1, CENTER = 2
		Start_Quest.setText(director.button_text());
		Start_Quest.setPosition(22*width/100, height/15);
		Start_Quest.setSize(20*width/100, height/10);
		stage.addActor(Start_Quest);
		//stage.addActor(Start_Quest).width(width/6).height(height/6).pad(10).right();
		//table.row();
		Start_Quest.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				if(director.get_index() == 2){
					logger.print("Class: " + director.get_class());
					logger.print("Level: " + director.get_level());
					logger.print("Race: " + director.get_race());
					game.game_vars = new ClassReferenceDrawable(director.get_class(),director.get_level(),director.get_race(),game.unlockable);
					game.gameScreen = new ActivityGame(game);
					game.setScreen(game.gameScreen);
				}else{
					director.iterate_next();
					init_actors();
				}
			}
		});
		
		final TextButton back_button = new TextButton("Back", skin.get_button_skin());
		//LEFT = 0, RIGHT = 1, CENTER = 2
		back_button.setPosition(3*width/100, height/15);
		back_button.setSize(width/6, height/10);
		stage.addActor(back_button);
		//stage.addActor(Start_Quest).width(width/6).height(height/6).pad(10).right();
		//table.row();
		back_button.addListener(new ChangeListener() {
			public void changed (ChangeEvent event, Actor actor) {
				/*if(director.get_index() == 2){
					game.game_vars = new ClassReferenceDrawable(director.get_class(),director.get_level(),director.get_race());
					game.gameScreen = new ActivityGame(game);
					game.setScreen(game.gameScreen);
				}else{*/
				if(director.get_index() == 0){
					game.setScreen(game.indexScreen);
				}else{
					director.iterate_previous();
					Start_Quest.setText(director.button_text());
					init_actors();
				}
				//}
			}
		});
		
		//The preview window
		preview_window = new ClassPreviewActor(new Point((int)(9*width)/20,(int)(1*height/10)),
				new Point((int)width/5,(int)(4*height/6)));
		
		//The smaller select windows
		//ClassGalleryActor classes = new ClassGalleryActor(sprite_textures.return_all_by_name("class"),
		//		new Point((int)width/3,(int)(4*height/6)),new Point((int)width/20,(int)(13*height/20)),7,3);
		stage.addActor(preview_window);
		
		
		director.set_vars(director.get_starting_index(),
				director.get_gallery().get_actors().get(director.get_starting_index()).get_name(),
				director.get_gallery().get_actors().get(director.get_starting_index()).get_description());
		
		int level_index = director.get_level() == -1?0:director.get_level();
		int current_completes = game.unlockable.get_unlockable_prototypes().get_factory().current_completes(level_index);
		int completes_needed = game.unlockable.get_unlockable_prototypes().get_factory().get_dungeon_completes()[level_index];
		int completes_left = completes_needed - current_completes;
		completes_left = completes_left < 0 ? 0 : completes_left;
		
		director.set_label_text(true, completes_left);
		preview_window.set_sprite(director.get_gallery().get_actors().get(director.get_starting_index()).get_sprite());
		
		//Sets a click action for all the sprites on the left so that the preview image can be seen for them
		//Also adds them to the stage
		for(final ClassActorFactory actor: director.get_gallery().get_actors()){
			final Sprite sprite = actor.get_sprite();
			//director.set_text(director.get_gallery().get_actors().indexOf(actor),actor.get_name(),actor.get_description());
			actor.addListener( new ClickListener() {     
				public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
			    {
					int level_index = director.get_level() == -1?0:director.get_level();
					int current_completes = game.unlockable.get_unlockable_prototypes().get_factory().current_completes(level_index);
					int completes_needed = game.unlockable.get_unlockable_prototypes().get_factory().get_dungeon_completes()[level_index];
					int completes_left = completes_needed - current_completes;
					completes_left = completes_left < 0 ? 0 : completes_left;
					
					director.set_vars(director.get_gallery().get_actors().indexOf(actor),actor.get_name(),actor.get_description());
					director.set_label_text(true, completes_left);
			    	preview_window.set_sprite(sprite);
			        return true;
			    }
			});
			//This adds the colored boxes around the characters
			boolean completed = game.unlockable.get_unlockable_prototypes().get_factory().level_beaten(director.get_level(), actor.get_index());
			stage.addActor(actor.set_completed_background(game.sprite_textures.find_by_name("icon_movement_selector"),
					game.sprite_textures.find_by_name("icon_dark_half"),completed));
			stage.addActor(actor);
		}
	}

}
